<?php
class Silk_Qslb_Helper_Data extends Mage_Core_Helper_Abstract
{	
	protected $_http_client;
	private static $_redis = null;

    protected function _http() {
        if (!$this->_http_client) {
            require_once "Zend/Http/Client.php";
            $this->_http_client = new Zend_Http_Client;
        }
        return $this->_http_client;
    }

    public function getRedis()
    {
    	if(self::$_redis) return self::$_redis;
    	$node = Mage::getConfig()->getNode('global/redis');
    	if($node)
    	{
    		$redis = $node->asArray();
    		if($redis && isset($redis['host']) && $redis['host'])
	    	{
	    		self::$_redis = new Credis_Client($redis['host'], 6379, 15, 'mage');
	    		return self::$_redis;
	    	}
    	}
    	self::$_redis = new Credis_Client('127.0.0.1', 6379, 15, 'mage');
    	return self::$_redis;
    }


	public function _getwebsiteId(){
		$websiteId = Mage::getSingleton('admin/session')->getUser()->getWebsiteId();
		return $websiteId;
	}

	public function getStore($id)
	{
		$cacheKey = "store:{$id}";
		$data = $this->getRedis()->hGetAll($cacheKey);
		if($data) return $data;


		$infoUrl = Mage::getStoreConfig('general/api/customer_push_url');
        $infoUrl = trim($infoUrl,'/').'/shops/'.$id;

        $response = $this->_http()
            ->setUri($infoUrl)
            ->setHeaders("Content-Type", "application/json")
            ->request(Zend_Http_Client::GET);
        if(!$response) return array();
        $html = $response->getBody();
        if(!$html) return array();

        $data = Zend_Json::decode($html);
        if($data && isset($data['code']) && $data['code'] == 200)
        {
        	return $data['data'];
        }
        else
        {
        	return array();
        }
	}
}
	 